package com.ronri.ams.sample;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.ronri.choosephotos.PhotoItem;
import com.ronri.common.FileManager;
import com.ronri.sign.LinePathView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * 文件：ams.java
 * 包名：com.ronri.ams.sample;   --- 正式发布时需要修正
 * 说明：轮播组件
 * 作者：MS During
 * 日期：2017/5/12
 * 历史：
 */
public class activity_control_sign extends abstract_activity {

    // 签名路径
    private String mSignPath = "";

    // 前面视图
    private LinePathView mSign = null;

    // 清除按钮
    private TextView mbtnClear = null;

    // 是否已经按下清除按钮
    private Boolean mbClear = false;

    // 签字列表
    private List<PhotoItem> mItems = new ArrayList<PhotoItem>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.control_sign_box);
        Log.i("aaa","activity_control_sign");
        Bundle bundle = getIntent().getExtras();
        String szTitle = bundle.getString("title");
        String szvTitle = bundle.getString("vtitle");
        String szFile = bundle.getString("filename");

        // 显示标题与右边功能
        setTitle(szTitle, szvTitle);

        // 初始化内容
        init();

        // 签名视图
        mSign = (LinePathView)findViewById(R.id.signView);

        // 设置或生成文件路径
        if (szFile == null) {
            mSignPath = InitApplication.PhotoSavePath + "original/" + FileManager.getTempFileName(".png");
        }else{
            mSignPath = szFile;
            // 加载图片
            mSign.loadSign(szFile);
        }

        // 清除按钮
        mbtnClear = (TextView)findViewById(R.id.clearSign);

        // 绑定清除按钮动作
        mbtnClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mSign != null) {
                    mSign.clear();
                    mbClear = true;
                }
            }
        });
    }

    @Override
    public void CallBackResult(String szString) {
        super.CallBackResult(szString);
    }

    /***
     * 确认提交
     * @return
     */
    @Override
    public boolean SureReturn() {

        if(mSign != null) {
            try {
                if(mSign.getTouched()) {

                    PhotoItem item = new PhotoItem();
                    item.PhotoPath = mSignPath;

                    mItems.add(item);

                    // 保存照片
                    mSign.save(mSignPath);

                    mSign.destory();
                    mSign.destroyDrawingCache();

                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();

                    // 结果
                    String resultString = JSON.toJSONString(mItems);

                    // 释放内存
                    mItems.clear();

                    bundle.putString("returnValue", resultString);
                    intent.putExtras(bundle);

                    this.setResult(RESULT_OK, intent);
                }else{
                    if(mbClear == true){
                        PhotoItem item = new PhotoItem();
                        item.PhotoPath = mSignPath;

                        mItems.add(item);

                        // 保存照片
                        mSign.save(mSignPath);

                        mSign.destory();
                        mSign.destroyDrawingCache();

                        Intent intent = new Intent();
                        Bundle bundle = new Bundle();

                        // 结果
                        String resultString = JSON.toJSONString(mItems);

                        // 释放内存
                        mItems.clear();

                        bundle.putString("returnValue", resultString);
                        intent.putExtras(bundle);

                        this.setResult(RESULT_OK, intent);
                    }
                    // 提示未签名
                }
            }catch(IOException ex){

            }
        }

        return super.SureReturn();
    }

    /***
     * 放弃保存
     * @return
     */
    @Override
    public boolean CancelReturn() {
        return super.CancelReturn();
    }
}
